package YuanFuDao2;

/**
 *
 */
public class Mian5_2 {

    public static void main(String[] args) {
        int minN = -1;
        int minM = -1;
        int maxN;
        int maxM;
        StringBuilder res = new StringBuilder();
        int[][] nums = new int[][]{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}, {16, 17, 18, 19, 20}};
        maxN = nums.length;
        maxM = nums[0].length;
        int total = maxN * maxM;   // 总的要取得个数

        int flag = 0; // 0：向下  1：向右   2：向上  3：向左
        while(total!=0){
            switch (flag){
                case 0:
                    minM++;
                    for (int i = minN+1; i < maxN; i++) {
                        res.append(nums[i][minM]).append(" ");
                    }
                    total--;
                    break;
                case 1:
                    maxN--;
                    for (int j = minM+1; j < maxM; j++) {
                        res.append(nums[maxN][j]).append(" ");
                    }
                    total--;
                    break;
                case 2:
                    maxM--;
                    for (int i = maxN-1; i > minN; i--) {
                        res.append(nums[i][maxM]).append(" ");
                    }
                    total--;
                    break;
                case 3:
                    minN++;
                    for (int j = maxM-1; j > minM; j--) {
                        res.append(nums[minN][j]).append(" ");
                    }
                    total--;
                    break;
            }
            flag = (flag + 1)%4;
        }

        String str = res.toString();
        System.out.println(str.substring(0,str.length()-1));
    }

}
